package jal4eclipse.ui.editors;

import jal4eclipse.jal.JALParser;
import jal4eclipse.ui.outline.JALOutline;

import java.util.ResourceBundle;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.TextOperationAction;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;


public class JALEditor extends TextEditor {
	private static final String CONTENTASSIST_PROPOSAL_ID = "jpicshell.JALEditor.ContentAssistProposal";
	private JALOutline outline;
	private StyledText textWidget;
	public JALEditor() {
		super();
		setSourceViewerConfiguration(new JALEditorConfiguration(this));
		outline = new JALOutline(this);
		
	}

	public void dispose() {
		super.dispose();
		
	}
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.AbstractTextEditor#createActions()
	 */
	protected void createActions() {
		super.createActions();
		// This action will fire a CONTENTASSIST_PROPOSALS operation
		// when executed
		IAction action = new TextOperationAction(
				ResourceBundle.getBundle(JALEditor.class.getPackage().getName()
						+ ".messages"), "ContentAssistProposal", this,
				SourceViewer.CONTENTASSIST_PROPOSALS);
		action.setActionDefinitionId(CONTENTASSIST_PROPOSAL_ID);
		// Tell the editor about this new action
		setAction(CONTENTASSIST_PROPOSAL_ID, action);
		// Tell the editor to execute this action
		// when Ctrl+Spacebar is pressed
		setActionActivationCode(CONTENTASSIST_PROPOSAL_ID, ' ', -1, SWT.CTRL);
		
		
	}

	protected ISourceViewer createSourceViewer(Composite parent,
			IVerticalRuler ruler, int styles) {
        ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);
        textWidget = viewer.getTextWidget();
        textWidget.addKeyListener(new JALEditorKeyEvent(this));
        return viewer;
	}

	public IDocument getDocument() {
		IDocumentProvider documentProvider = getDocumentProvider();
		if (documentProvider != null) {
			return documentProvider.getDocument(getEditorInput());
		}
		return null;
	}

	public Object getAdapter(Class adapter) {
		if (adapter == IContentOutlinePage.class) {
			return outline;
		}
		return super.getAdapter(adapter);
	}
	
	public void doSave(IProgressMonitor progressMonitor) {
		super.doSave(progressMonitor);
		// Rebuild outline
		String content = getDocument().get();
		TreeViewer tree = outline.getViewer(); 
		tree.setInput(JALParser.getFileStructure(content));
		tree.expandAll();
		
	}
	
	
	
	
	
	

	public JALOutline getOutline() {
		return outline;
	}

	public void setOutline(JALOutline outline) {
		this.outline = outline;
	}

	public StyledText getTextWidget() {
		return textWidget;
	}

	public void setTextWidget(StyledText textWidget) {
		this.textWidget = textWidget;
	}
	

}
